package com.itheima.sh.a_break_01.homework;

import java.util.Random;
import java.util.Scanner;

public class Test05 {
    public static void main(String[] args) {
       //创建随机数类对象
        Random r=new Random();
        //创建一个长度为20的动态数组
        int [] arr=new int[20];
        //遍历数组
        for (int i = 0; i < 20; i++) {
            //生成1-20间的随机数
            int number=r.nextInt(21)+20;
            arr[i]=number;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
        //定义计数器
        int count=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入20-40之间的整数：");
        int num= sc.nextInt();
        while (true){
            if (num>=20&&num<=40){
                break;
            }else {
                System.out.println("您输入的不是20-40之间的整数、请重新输入：");
                num=sc.nextInt();
            }
        }
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==num){
                count++;
            }
        }
        System.out.println("数组中元素" + num+"出现的次数："+count+"次");
    }
}
